package filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月23日 16:44
 * @Description TODO
 */
@WebFilter(value = "/*",initParams = {
        @WebInitParam(name = "charset",value = "utf-8")
})
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        过滤器初始化执行方法
        System.out.println( "filter过滤器："+filterConfig.getInitParameter("charset") );
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        过滤器的执行体方法
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        req.setCharacterEncoding("utf-8");
        servletResponse.setCharacterEncoding("utf-8");
        System.out.println( req.getServletPath() + "被访问了" );
//        放行请求 - 如果过滤器中不存在该方法执行，所有的请求都会被拦截，且不能正常方法
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {
//        过滤器的销毁方法
        System.out.println("filer destroy");
    }
}
